function dF = ffunPortfolio(V,F, r, gamma ,Lambda, phi,q,aBar,delta,VB,vVPostx,vdFPost,vFPost,FB)
        
%Solve HJB equation for the loan portfolio case, i.e., with two loans
%Solve the model taking the solution after the default as given

        dF=zeros(2,1);
        dF(1)=F(2);
        
        F1=F(1);

        [m,ind]=min(abs(2*vdFPost-dF(1)));

        

        VPost=vVPostx(ind);
        FPost=vFPost(ind);
     

       


        
        a=(2*(F1-FPost)-(gamma-r)*phi-dF(1)*(2*V+phi)+dF(1)*VPost)/(2*phi);

        if (a<0)
            
            a=0;
          
            
        end
        
        if (a>aBar)
            
            a=aBar;
           
            
        end

        if (phi*a>F1)

            a=F1/phi;
            lambda=Lambda-q-a;
            W=phi*a;
            dV=(gamma+2*lambda)*V-W;
            dF(2)=(dF(1)^2-dF(1)*F1/phi+dF(1)^2*V/phi)/(min(dV,-0.0001));
            dF(2)=min(dF(2),-0.0001);

        else
        
            lambda=Lambda-q-a;
            W=phi*a;
            dV=(gamma+2*lambda)*V-W-lambda*VPost;
         
            
            dF(2)=-(gamma-r)*dF(1)/(min(dV,-0.00001));
            dF(2)=min(dF(2),-0.0001);
            
        end

end